<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $scripturl, $txt, $user_info, $context;
global $dbtables, $newbie_nice;

// Check if user already has a BNT account
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);

// If the user already has an account, run the pre-login checks
if ($smcFunc['db_num_rows']($result) <> 0)
{
	// Get the player information from the very first query
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	// Check the players IP againt the database for an IP ban
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_bans}
		WHERE {string:ip} LIKE ban_mask',
		array(
			'db_bans' => $dbtables['ip_bans'],
			'ip' => $_SERVER['REMOTE_ADDR']
		)
	);
	
	// If there is a match, kill the login process.
	if (($smcFunc['db_num_rows']($result)) != 0 )
	{
		echo '
					<p>', $txt['bnt_login_banned'], '<p>';
		return;
	}
	$smcFunc['db_free_result']($result);
	
	// Check if player's ship has not been destroyed
	if ($playerinfo['ship_destroyed'] == "N")
	{
		// Player's ship was not destroyed, log the login to the players log and redirect to main menu.
		//playerlog($playerinfo['ship_id'], LOG_LOGIN, $_SERVER['REMOTE_ADDR']);
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET last_login = NOW(),
				ip_address = {string:ip_address}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ip_address' => $_SERVER['REMOTE_ADDR'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		TextMainMenu();
		redirectexit('action=blacknova;sa=main');
	}
	else
	{	
		// Player had a ship . . . it was destroyed, but the player had an escape pod.
		if ($playerinfo['dev_escapepod'] == "Y")
		{
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET hull = 0,
					engines = 0,
					power = 0,
					computer = 0,
					sensors = 0,
					beams = 0,
					torp_launchers = 0,
					torps = 0,
					armor = 0,
					armor_pts = 100,
					cloak = 0,
					shields = 0,
					sector = 0,
					ship_ore = 0,
					ship_organics = 0,
					ship_energy = 500,
					ship_colonists = 0,
					ship_goods = 0,
					ship_fighters = 100,
					on_planet = "N",
					dev_warpedit = 0,
					dev_genesis = 0,
					dev_beacon = 0,
					dev_emerwarp = 0,
					dev_escapepod = "N",
					dev_fuelscoop = "N",
					dev_minedeflector = 0,
					ship_destroyed = "N",
					dev_lssd = "N"
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'ship_id' => $playerinfo['ship_id']
				)
			);
			
			$txt['bnt_login_died'] = str_replace("[scripturl]", $scripturl, $txt['bnt_login_died']);
			echo '
					<p>', $txt['bnt_login_died'], '</p>';
			TextMainMenu();
		}
		// Player had a ship, it was destroyed but the player had no escape pod.
		else
		{
			$txt['bnt_login_dead'] = str_replace("[scripturl]", $scripturl, $txt['bnt_login_dead']);
			echo '
					<p>', $txt['bnt_login_dead'], '</p>';

			// Check if $newbie_nice is set, if so, verify ship limits
			if ($newbie_nice == "YES")
			{
				global $newbie_hull, $newbie_engines, $newbie_power, $newbie_computer;
				global $newbie_sensors, $newbie_armor, $newbie_shields, $newbie_beams;
				global $newbie_torp_launchers, $newbie_cloak;
			
				$result = $smcFunc['db_query']('', '
					SELECT hull, engines, power, computer, sensors, armor, shields, beams, torp_launchers, cloak
					FROM {raw:db_ships}
					WHERE ship_id = {int:ship_id}
					AND hull <= {int:newbie_hull} 
					AND engines <= {int:newbie_engines}
					AND power <= {int:newbie_power} 
					AND computer <= {int:newbie_computer} 
					AND sensors <= {int:newbie_sensors}
					AND armor <= {int:newbie_armor} 
					AND shields <= {int:newbie_shields} 
					AND beams <= {int:newbie_beams}
					AND torp_launchers <= {int:newbie_torp_launchers} 
					AND cloak <= {int:newbie_cloak}
					LIMIT 1',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $playerinfo['ship_id'],
						'newbie_hull' => $newbie_hull,
						'newbie_engines' => $newbie_engines,
						'newbie_power' => $newbie_power,
						'newbie_computer' => $newbie_computer,
						'newbie_sensors' => $newbie_sensors,
						'newbie_armor' => $newbie_armor,
						'newbie_shields' => $newbie_shields,
						'newbie_beams' => $newbie_beams,
						'newbie_torp_launchers' => $newbie_torp_launchers,
						'newbie_cloak' => $newbie_cloak
					)
				);
				
				if ($smcFunc['db_num_rows']($result) > 0)
				{
					// Player was still in newbie status, give them another chance
					echo '
					<p>', $txt['bnt_login_newbie'], '</p>';
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET hull = 0,
							engines = 0,
							power = 0,
							computer = 0,
							sensors = 0,
							beams = 0,
							torp_launchers = 0,
							torps = 0,
							armor = 0,
							armor_pts = 100,
							cloak = 0,
							shields = 0,
							sector = 0,
							ship_ore = 0,
							ship_organics = 0,
							ship_energy = 500,
							ship_colonists = 0,
							ship_goods = 0,
							ship_fighters = 100,
							credits = 1000,
							on_planet = "N",
							dev_warpedit = 0,
							dev_genesis = 0,
							dev_beacon = 0,
							dev_emerwarp = 0,
							dev_escapepod = "N",
							dev_fuelscoop = "N",
							dev_minedeflector = 0,
							ship_destroyed = "N",
							dev_lssd = "N"
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id']
						)
					);
					$txt['bnt_login_newlife'] = str_replace("[link]", '', $txt['bnt_login_newlife']);
					echo '
					<p>', $txt['bnt_login_newlife'], '</p>';
				}
				else
				{
					// Player starts over, treat as new player creation.
					$txt['bnt_login_newlife'] = str_replace("[link]", '', $txt['bnt_login_newlife']);
					echo '
					<p>', $txt['bnt_login_loser'], '</p>
					<p>', $txt['bnt_login_newlife'], '</p>';
				}
				$smcFunc['db_free_result']($result);
			}
			else
			{
				// Newbie nice is not set, player is dead and has to start over.
				$txt['bnt_login_newlife'] = str_replace("[link]", '', $txt['bnt_login_newlife']);
				echo '
					<p>', $txt['bnt_login_loser'], '</p>
					<p>', $txt['bnt_login_newlife'], '</p>';
			}
		}
	}
}
else
{
	// This is a new player, display the character creation screen.
	redirectexit('action=blacknova;sa=createplayer');
}
?>
